package org.dromara.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.setting.domain.bo.SettingIconConfigBo;
import org.dromara.setting.domain.vo.SettingIconConfig;
import org.dromara.setting.domain.vo.SettingIconConfigVo;
import org.dromara.setting.mapper.SettingIconConfigMapper;
import org.dromara.setting.service.ISettingIconConfigService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author xl
 * @description 针对表【setting_icon_config(图标配置)】的数据库操作Service实现
 * @createDate 2024-08-14 18:53:38
 */
@RequiredArgsConstructor
@Service
public class SettingIconConfigServiceImpl implements ISettingIconConfigService {

    private final SettingIconConfigMapper baseMapper;

    @Override
    public TableDataInfo<SettingIconConfigVo> queryPageList(SettingIconConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SettingIconConfig> lqw = buildQueryWrapper(bo);
        Page<SettingIconConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<SettingIconConfigVo> queryList(SettingIconConfigBo bo) {
        LambdaQueryWrapper<SettingIconConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public SettingIconConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public Boolean insertByBo(SettingIconConfigBo bo) {
        SettingIconConfig add = MapstructUtils.convert(bo, SettingIconConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(SettingIconConfigBo bo) {
        SettingIconConfig update = MapstructUtils.convert(bo, SettingIconConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SettingIconConfig entity) {
    }

    private LambdaQueryWrapper<SettingIconConfig> buildQueryWrapper(SettingIconConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SettingIconConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(SettingIconConfig::getSortSum);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SettingIconConfig::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            SettingIconConfig::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.like(StringUtils.isNotBlank(bo.getName()), SettingIconConfig::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getRedirectUrl()), SettingIconConfig::getRedirectUrl, bo.getRedirectUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationShow()), SettingIconConfig::getLocationShow, bo.getLocationShow());
        lqw.like(StringUtils.isNotBlank(bo.getRedirectType()), SettingIconConfig::getRedirectType, bo.getRedirectType());
        lqw.eq(bo.getShowStatus() != null, SettingIconConfig::getShowStatus, bo.getShowStatus());
        return lqw;
    }
}




